import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Polygon;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.speljohan.rsbot.accessors.Player;
import com.speljohan.rsbot.bot.Bot;
import com.speljohan.rsbot.event.events.ServerMessageEvent;
import com.speljohan.rsbot.event.listeners.PaintListener;
import com.speljohan.rsbot.event.listeners.ServerMessageListener;
import com.speljohan.rsbot.script.Calculations;
import com.speljohan.rsbot.script.Constants;
import com.speljohan.rsbot.script.Script;
import com.speljohan.rsbot.script.Skills;
import com.speljohan.rsbot.script.wrappers.RSArea;
import com.speljohan.rsbot.script.wrappers.RSInterface;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSObject;
import com.speljohan.rsbot.script.wrappers.RSPlayer;
import com.speljohan.rsbot.script.wrappers.RSTile;

public class MouchiccSW extends Script implements ServerMessageListener,
		PaintListener {

	// ***Stuff for the Blue Team***\\
	private RSTile[] BlueToBandage = { new RSTile(1807, 3232),
			new RSTile(1806, 3242), new RSTile(1808, 3254) };
	private RSTile BlueWaitDoor = new RSTile(1879, 3162);
	private RSTile blueToWestPyre[] = { new RSTile(1806, 3230),
			new RSTile(1808, 3240), new RSTile(1815, 3247),
			new RSTile(1825, 3244), new RSTile(1835, 3244),
			new RSTile(1844, 3244), new RSTile(1849, 3248) };
	private RSTile blueToObelisk[] = { new RSTile(1806, 3234),
			new RSTile(1810, 3246), new RSTile(1823, 3245),
			new RSTile(1832, 3241), new RSTile(1843, 3234),
			new RSTile(1857, 3233), new RSTile(1870, 3233),
			new RSTile(1882, 3232) };
	private RSTile[] blueToAvatar = { new RSTile(1807, 3237),
			new RSTile(1815, 3248), new RSTile(1828, 3245),
			new RSTile(1834, 3243), new RSTile(1847, 3238),
			new RSTile(1861, 3233), new RSTile(1876, 3233),
			new RSTile(1892, 3232), new RSTile(1907, 3231),
			new RSTile(1922, 3231), new RSTile(1936, 3228),
			new RSTile(1948, 3220), new RSTile(1962, 3214),
			new RSTile(1967, 3229), new RSTile(1967, 3244) };

	// ***Stuff for the Red Team***\\
	private RSTile[] RedToBandage = { new RSTile(1967, 3237),
			new RSTile(1968, 3225), new RSTile(1969, 3216),
			new RSTile(1966, 3209) };
	private RSTile RedWaitDoor = new RSTile(1900, 3162);
	private RSTile redToEastPyre[] = { new RSTile(1967, 3232),
			new RSTile(1966, 3220), new RSTile(1958, 3211),
			new RSTile(1951, 3215), new RSTile(1942, 3222),
			new RSTile(1931, 3218), new RSTile(1924, 3211) };
	private RSTile redToObelisk[] = { new RSTile(1967, 3230),
			new RSTile(1966, 3219), new RSTile(1956, 3211),
			new RSTile(1946, 3218), new RSTile(1935, 3224),
			new RSTile(1921, 3225), new RSTile(1911, 3231),
			new RSTile(1902, 3231), new RSTile(1894, 3231),
			new RSTile(1887, 3232) };
	private RSTile[] redToAvatar = { new RSTile(1967, 3229),
			new RSTile(1962, 3214), new RSTile(1948, 3220),
			new RSTile(1936, 3228), new RSTile(1922, 3231),
			new RSTile(1907, 3231), new RSTile(1892, 3232),
			new RSTile(1876, 3233), new RSTile(1861, 3233),
			new RSTile(1847, 3238), new RSTile(1834, 3243),
			new RSTile(1828, 3245), new RSTile(1815, 3248),
			new RSTile(1807, 3237), new RSTile(1807, 3224),
			new RSTile(1807, 3215) };

	// ***Stuff for West Graveyard***\\
	private RSTile GWestToWestPyrefiends[] = { new RSTile(1845, 3231),
			new RSTile(1849, 3242), new RSTile(1849, 3242) };
	private RSTile GWestToObelisk[] = { new RSTile(1853, 3226),
			new RSTile(1864, 3234), new RSTile(1875, 3233),
			new RSTile(1885, 3231) };

	public String Wayoo = "";

	// ***Stuff for East Graveyard***\\
	private RSTile GEastToEastPyrefiends[] = { new RSTile(1925, 3232),
			new RSTile(1926, 3221), new RSTile(1928, 3212) };
	private RSTile GEastToObelisk[] = { new RSTile(1924, 3233),
			new RSTile(1913, 3231), new RSTile(1903, 3230),
			new RSTile(1894, 3232), new RSTile(1887, 3231) };

	// ***Stuff for General Use***\\
	public RSTile[] ToRed = { new RSTile(1890, 3164), new RSTile(1899, 3162) };
	public RSTile[] ToBlue = { new RSTile(1890, 3164), new RSTile(1880, 3162) };
	RSTile bandageTableBlue = new RSTile(1810, 3254);
	RSTile bandageTableRed = new RSTile(1963, 3207);
	private RSTile BalancePortal = new RSTile(1890, 3162);
	private RSNPC PyreFiend = getNearestNPCByID(8598);
	private String theStuff = "";
	private RSNPC Barricade = getNearestNPCByID(8600, 8601);

	// ***Ints and Longs***\\
	int Dobble;
	private int bandageID = 14640;
	private int deathTime = 0;
	private int zealGained = 0;
	private int gamesCompleted = 0;
	private int soulShards = 0;
	long lvls = 0;
	long startTime = System.currentTimeMillis();
	int startLvl;
	int startExp;

	// ***Booleans***\\
	private boolean leftSpawn = false;
	private boolean randomJoin = false;
	private boolean onlyBalance = false;
	private boolean onlyRed = false;
	private boolean onlyBlue = false;
	private boolean onlyWinners = false;
	private boolean onlyLosers = false;
	private boolean KILL = false;
	private boolean AFK = false;
	private boolean FIGHT = false;
	private boolean SPECIAL = false;
	private boolean inGame = false;
	private boolean SUICIDE = false;

	// ***Strings***\\
	private String currentTeam = null;
	private String styleOfGame = null;
	private String lastWon = null;
	private String whatJoin = null;

	public MouchiccSW() {
		super();
		setParameter("author", "Mouchicc - MysticTeam");
		setParameter("version", "1.4");
		setParameter("name", "MouchiccSW");
		setParameter("category", "Combat");
	}

	public String getName() {
		return "MouchiccSW";
	}

	public String getAuthor() {
		return "Mouchicc - MysticTeam";
	}

	public String getScriptCategory() {
		return "Combat";
	}

	public double getVersion() {
		return 1.4;
	}

	public String getScriptDescription() {
		String html = "<html><head>";
		html += "</head><body>";
		html += "<center><strong><h2>" + getName() + " v" + getVersion()
				+ "</h2></strong></center>";
		html += "<center>Start the script near the Blance Portal area.</center>";
		html += "<strong>What team should we join?</strong><br />";
		html += "<select name='whatJoin'>";
		html += "<option>Random Joining";
		html += "<option>Won Last";
		html += "<option>Lost Last";
		html += "<option>Balance";
		html += "<option>Join Blue";
		html += "<option>Join Red</select><br />";
		html += "<input name='optionAFK' type='checkbox' value='1'>AFK<br />";
		html += "<input name='optionKILL' type='checkbox' value='1'>Kill Players<br />";
		html += "<input name='optionFIGHT' type='checkbox' value='1'>Fight Monsters<br />";
		html += "<input name='optionSPECIAL' type='checkbox' value='1'>Special Attack<br />";
		html += "</body></html>";
		html += "";
		return html;
	}

	@Override
	public boolean onStart(Map<String, String> args) {
		AFK = args.get("optionAFK") != null ? true : false;
		KILL = args.get("optionKILL") != null ? true : false;
		FIGHT = args.get("optionFIGHT") != null ? true : false;
		SPECIAL = args.get("optionSPECIAL") != null ? true : false;

		if (AFK) {
			styleOfGame = "AFK";
		} else if (FIGHT) {
			styleOfGame = "Fight Monsters";
		} else if (KILL) {
			styleOfGame = "Kill Players";
		}

		whatJoin = args.get("whatJoin");
		if (whatJoin.equals("Random Joining")) {
			randomJoin = true;
		} else if (whatJoin.equals("Won Last")) {
			onlyWinners = true;
		} else if (whatJoin.equals("Lost Last")) {
			onlyLosers = true;
		} else if (whatJoin.equals("Balance")) {
			onlyBalance = true;
		} else if (whatJoin.equals("Join Blue")) {
			currentTeam = "Blue";
			onlyBlue = true;
		} else if (whatJoin.equals("Join Red")) {
			currentTeam = "Red";
			onlyRed = true;
		} else {
			log("Team Selection Error!");
			return false;
		}
		return true;
	}

	// Types a random number
	public void enterNumber() {
		if (RSInterface.getInterface(752).getChild(4).isValid()
				&& RSInterface.getInterface(752).getChild(4).containsText(
						"How many bandages would")) {
			wait(random(400, 800));
			int rand = random(28, 999);
			Bot.getInputManager().sendKeys(Integer.toString(rand), true);
		}
	}

	private boolean leaveGRRed() {
		RSTile RedWaitDoor = new RSTile(1959, 3239);
		if (RedWaitDoor != null) {
			if (distanceTo(new RSTile(1960, 3239)) < 2) {
				if (!getMyPlayer().isMoving()) {
					setCameraAltitude(false);
					setCompass('e');
					if (clickTile(RedWaitDoor, "Blue barrier")) {
						wait(random(500, 750));
					}
				}
			} else {
				walkTileMM(randomizeTile(new RSTile(1960, 3239), 2, 2));
			}
			if (getMyPlayer().getLocation().getX() == 1959
					&& getMyPlayer().getLocation().getY() == 3239) {
				log("We have left the spawn!");
				currentTeam = "Red";
				inGame = true;
				return true;
			}
		}
		return false;
	}

	private boolean leaveGRBlue() {
		RSTile BlueWaitDoor = new RSTile(1815, 3225);
		if (BlueWaitDoor != null) {
			if (distanceTo(new RSTile(1816, 3225)) < 2) {
				if (!getMyPlayer().isMoving()) {
					setCameraAltitude(false);
					setCompass('e');
					if (clickTile(BlueWaitDoor, "Blue barrier")) {
						wait(random(500, 750));
					}
				}
			} else {
				walkTileMM(randomizeTile(new RSTile(1816, 3225), 2, 2));
			}
			if (getMyPlayer().getLocation().getX() == 1815
					&& getMyPlayer().getLocation().getY() == 3225) {
				log("We have left the spawn!");
				currentTeam = "Blue";
				inGame = true;
				return true;
			}
		}
		return false;
	}

	public RSTile[] generatePath(int destinationX, int destinationY) {
		return generatePath(getMyPlayer().getLocation().getX(), getMyPlayer()
				.getLocation().getY(), destinationX, destinationY);
	}

	private RSTile[] generatePath(int startX, int startY, int destinationX,
			int destinationY) { // most credits to aftermath
		double dx, dy;
		ArrayList<RSTile> list = new ArrayList<RSTile>();

		list.add(new RSTile(startX, startY));
		while (Math.hypot(destinationY - startY, destinationX - startX) > 8) {
			dx = destinationX - startX;
			dy = destinationY - startY;
			int gamble = random(14, 17);
			while (Math.hypot(dx, dy) > gamble) {
				dx *= .95;
				dy *= .95;
			}
			startX += (int) dx;
			startY += (int) dy;
			list.add(new RSTile(startX, startY));
		}
		list.add(new RSTile(destinationX, destinationY));
		return list.toArray(new RSTile[list.size()]);
	}

	private void checkTheTeamNow() {
		if (!inGame || leftSpawn) {

			if (!inGame) {

				if (getMyPlayer().getLocation().getX() == 1815
						&& getMyPlayer().getLocation().getY() == 3225
						|| getMyPlayer().getLocation().getX() == 1959
						&& getMyPlayer().getLocation().getY() == 3239) {
					inGame = true;
				}

				if (currentTeam == "Red") {
					if (isRedStartingArea()) {
						log("The game has started!");
						currentTeam = "Red";
						inGame = true;
					}
				}

				if (currentTeam == "Blue") {
					if (isBlueStartingArea()) {
						log("The game has started!");
						currentTeam = "Blue";
						inGame = true;
					}
				}

				if (currentTeam == null) {
					if (isBlueStartingArea()) {
						log("We are in the game - Setting team to Blue!");
						currentTeam = "Blue";
						inGame = true;
					}
					if (isRedStartingArea()) {
						log("We are in the game - Setting team to Red!");
						currentTeam = "Red";
						inGame = true;
					}
				}
			} else {
				if (isAtRedRoom() || getMyPlayer().getLocation().getX() == 1900
						&& getMyPlayer().getLocation().getX() == 3162) {
					currentTeam = "Red";
					log("We are on the Red team.");

				}
				if (isAtBlueRoom()
						|| getMyPlayer().getLocation().getX() == 1879
						&& getMyPlayer().getLocation().getX() == 3162) {
					currentTeam = "Blue";
					log("We are on the Blue team.");

				}
			}
		}
	}

	public void MCRest(int stopEnergy) {
		int energy = getEnergy();
		if (energy < stopEnergy) {
			wait(random(250, 500));
			atInterface(INTERFACE_GAME_SCREEN, BUTTON_RUN, "Rest");
			energy = getEnergy();
		}
	}

	// TODO
	public int loop() {

		if (getMyPlayer().isMoving()) {
			return 800;
		}

		if (isRedStartingArea() || isBlueStartingArea()) {
			inGame = true;
		}

		if (!inGame) {

			if (distanceTo(BalancePortal) <= 10 && !isAtBlueRoom()
					&& !!isAtRedRoom()) {
				joinTheTeam();
			}

			if (isRedStartingArea()) {
				leaveGRRed();
				currentTeam = "Red";
			} else if (isBlueStartingArea()) {
				leaveGRBlue();
				currentTeam = "Blue";
			}

			if (isAtRedRoom()) {
				MCRest(100);
				currentTeam = "Red";
			} else if (isAtBlueRoom()) {
				MCRest(100);
				currentTeam = "Blue";
			}

			if (!inGame || leftSpawn) {
				checkTheTeamNow();
			}
		}

		if (inGame) {
			if (Barricade != null && distanceTo(Barricade) <= 1
					&& !getMyPlayer().isMoving()) {
				clickNPC(Barricade, "Attack");
			}

			// Lines for AFKing:

			if (AFK) {

				if (currentTeam.equals("Blue")) {
					AFKBlue();
				} else if (currentTeam.equals("Red")) {
					AFKRed();
				}
			} else

			// Lines for Fighting Monsters:

			if (FIGHT) {

				if (currentTeam.equals("Blue")) {
					FightPyres(blueToWestPyre);
				} else if (currentTeam.equals("Red")) {
					FightPyres(redToEastPyre);
				}
			} else

			// Lines for Killing Players:

			if (KILL) {

				if (currentTeam.equals("Blue")) {
					KillPlayers(blueToObelisk);
				} else if (currentTeam.equals("Red")) {
					KillPlayers(redToObelisk);
				}
			}

			// Lines for Suicide:

			if (SUICIDE) {

				if (currentTeam.equals("Blue")) {
					Suicide(blueToAvatar);
				} else if (currentTeam.equals("Red")) {
					Suicide(redToAvatar);
				}
			}

			if (isBlueGraveyard() || isRedGraveyard()) {
				setCameraAltitude(false);
				if (isBlueGraveyard()) {
					setCompass('s');
					RSTile blueGraveyardBarrier = new RSTile(1842, 3219);
					if (blueGraveyardBarrier != null) {
						if (clickTile(blueGraveyardBarrier, "barrier")) {
							wait(random(500, 750));
						}
					}
				}
				if (isRedGraveyard()) {
					setCompass('n');
					RSTile redGraveyardBarrier = new RSTile(1933, 3244);
					if (redGraveyardBarrier != null) {
						if (clickTile(redGraveyardBarrier, "barrier")) {
							wait(random(500, 750));
						}
					}
				}
			}

			if (infrontOfBlueGraveyard()) {
				if (currentTeam.equals("Blue")) {
					if (KILL) {
						walkPathMM(randomizePath(GWestToObelisk, 2, 2), 20);
					} else if (FIGHT) {
						walkPathMM(randomizePath(GWestToWestPyrefiends, 2, 2),
								20);
					} else if (SUICIDE) {
						walkPathMM(randomizePath(GWestToObelisk, 2, 2), 20);
					}
				}
			} else if (infrontOfRedGraveyard()) {
				if (currentTeam.equals("Red")) {
					if (KILL) {
						walkPathMM(randomizePath(GEastToObelisk, 2, 2), 20);
					} else if (FIGHT) {
						walkPathMM(randomizePath(GEastToEastPyrefiends, 2, 2),
								20);
					} else if (SUICIDE) {
						walkPathMM(randomizePath(GEastToObelisk, 2, 2), 20);
					}
				}
			}
		}
		return (random(50, 100));
	}

	// Joins the Red Team
	private int joinRedTeam() {
		try {
			RSObject enterRedBarrier = findObject(42030);

			if (playerInArea(1909, 3157, 1900, 3166)) {
				log("We have chosen the Red Team");
			}
			if (RSInterface.getInterface(211).isValid()) {
				atInterface(RSInterface.getInterface(211).getChild(3));
				return random(800, 1000);
			}
			if (RSInterface.getInterface(228).isValid()) {
				atInterface(RSInterface.getInterface(228).getChild(2));
				return random(800, 1000);
			}
			if (!playerInArea(1909, 3157, 1900, 3166)) {
				if (distanceTo(enterRedBarrier) > 3) {
					if (!getMyPlayer().isMoving()) {
						walkPathMM(randomizePath(ToRed, 2, 2), 20);
					}
					return (random(800, 1000));
				}
				if (getCameraAngle() < 260 || getCameraAngle() > 280) {
					setCameraRotation(random(260, 280));
					return (random(300, 500));
				}
				clickTile(RedWaitDoor, "pass");
				return (random(800, 1000));
			}

		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return random(50, 150);
	}

	// Join the Blue Team
	private int joinBlueTeam() {
		try {
			RSObject enterBlueBarrier = findObject(42029);

			if (playerInArea(1879, 3157, 1870, 3166)) {
				log("We have chosen the Blue Team");
			}
			if (RSInterface.getInterface(211).isValid()) {
				atInterface(RSInterface.getInterface(211).getChild(3));
				return random(800, 1000);
			}
			if (RSInterface.getInterface(228).isValid()) {
				atInterface(RSInterface.getInterface(228).getChild(2));
				return random(800, 1000);
			}
			if (!playerInArea(1879, 3157, 1870, 3166)) {
				if (distanceTo(enterBlueBarrier) > 3) {
					if (!getMyPlayer().isMoving()) {
						walkPathMM(randomizePath(ToBlue, 2, 2), 20);
					}
					return (random(800, 1000));
				}
				if (getCameraAngle() < 80 || getCameraAngle() > 100) {
					setCameraRotation(random(80, 100));
					return (random(300, 500));
				}
				clickTile(BlueWaitDoor, "pass");
				return (random(800, 1000));
			}
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return random(50, 150);
	}

	// Join at the Balance Portal
	public int joinBalance() {
		try {
			if (playerInArea(1909, 3157, 1900, 3166)) {
				log("We have chosen the Red Team");
			}

			if (playerInArea(1879, 3157, 1870, 3166)) {
				log("We have chosen the Blue Team");
			}

			if (RSInterface.getInterface(211).isValid()) {
				atInterface(RSInterface.getInterface(211).getChild(3));
				return random(800, 1000);
			}

			if (RSInterface.getInterface(228).isValid()) {
				atInterface(RSInterface.getInterface(228).getChild(2));
				return random(800, 1000);
			}

			if (!playerInArea(1879, 3157, 1870, 3166)) {
				if (!playerInArea(1909, 3157, 1900, 3166)) {
					if (distanceTo(BalancePortal) >= 3) {
						if (!getMyPlayer().isMoving()) {
							walkTileMM(BalancePortal.randomizeTile(2, 2));
						}
						return (random(800, 1000));
					}
					if (distanceTo(BalancePortal) < 3) {
						clickTile(BalancePortal, "join-team");
						return (random(800, 1000));
					}
				}
			}
		} catch (NullPointerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return random(50, 150);
	}

	// Joins the team you select
	private void joinTheTeam() {
		if (randomJoin) {
			int GambleInt = random(1, 3);
			if (GambleInt == 1) {
				joinRedTeam();
			} else if (GambleInt == 2) {
				joinBlueTeam();
			} else if (GambleInt == 3) {
				joinBalance();
			}
		}

		if (onlyRed) {
			joinRedTeam();
			return;
		}

		if (onlyBlue) {
			joinBlueTeam();
			return;
		}

		if (onlyBalance) {
			joinBalance();
			return;
		}

		if (onlyWinners) {
			if (lastWon == null) {
				joinBalance();
			}
			if (lastWon == "Red") {
				joinRedTeam();
			}
			if (lastWon == "Blue") {
				joinBlueTeam();
			}
			return;
		}

		if (onlyLosers) {
			if (lastWon == null) {
				joinBalance();
			}
			if (lastWon == "Red") {
				joinBlueTeam();
			}
			if (lastWon == "Blue") {
				joinRedTeam();
			}
			return;
		}
	}

	// Gets Special
	public int getSpec() {
		return getSetting(300);
	}

	// Gets if Special is Enabled
	public boolean specEnabled() {
		return (getSetting(301)) == 1;
	}

	// Does Special!
	public void doSpec() {
		if (getSpec() >= 1000 && !specEnabled() && SPECIAL == true) {
			openTab(Constants.TAB_ATTACK);
			clickMouse(645 + random(0, 4), 425 + random(0, 4), true);
			wait(350);
		}
		return;
	}

	String lastmessage = null;
	String checkMessage = null;

	// ServerMessage thingys
	public void serverMessageRecieved(ServerMessageEvent e) {
		String messageEvent = e.getMessage();

		if (messageEvent == lastmessage
				&& (!messageEvent.contains("you are dead") || !messageEvent
						.contains("gave you some soul fragments"))) {
			return;
		}

		lastmessage = e.getMessage();
		checkMessage = e.getMessage();

		if (messageEvent.contains("gave you some soul fragments")) {
			log("You have gained some soul fragments.");
			soulShards++;
		}
		if (messageEvent.contains("died")) {
			deathTime++;
		}
		if (messageEvent.contains("participation")) {
			gamesCompleted++;
			if (messageEvent.contains("3 Zeal")) {
				zealGained += 3;
				log("We have gained 3 Zeal.");
			} else if (messageEvent.contains("2 Zeal")) {
				zealGained += 2;
				log("We have gained 2 Zeal.");
			} else if (messageEvent.contains("1 Zeal")) {
				zealGained += 1;
				log("We have gained 1 Zeal.");
			}
			if (messageEvent.contains("was drawn")) {
				lastWon = null;
				log("It was a draw!");
				reset();
			} else if (messageEvent.contains("blue")) {
				lastWon = "Blue";
				log("Blue team won!");
				reset();
			} else if (messageEvent.contains("red")) {
				lastWon = "Red";
				log("Red team won!");
				reset();
			}
		}
	}

	int xpgained = 0;
	int skillATT = 0;
	int skillSTR = 0;
	int skillDEF = 0;
	int skillRNG = 0;
	int skillMAG = 0;
	int skillHIT = 0;
	int skillSLY = 0;
	int skillPRY = 0;
	int[] skillArray;
	String[] nameArray;

	private boolean setupSkills() {
		skillATT = skills.getCurrentSkillExp(Skills.getStatIndex("attack"));
		skillSTR = skills.getCurrentSkillExp(Skills.getStatIndex("strength"));
		skillDEF = skills.getCurrentSkillExp(Skills.getStatIndex("defence"));
		skillRNG = skills.getCurrentSkillExp(Skills.getStatIndex("range"));
		skillMAG = skills.getCurrentSkillExp(Skills.getStatIndex("magic"));
		skillHIT = skills.getCurrentSkillExp(Skills.getStatIndex("hitpoints"));
		skillSLY = skills.getCurrentSkillExp(Skills.getStatIndex("slayer"));
		skillPRY = skills.getCurrentSkillExp(Skills.getStatIndex("prayer"));
		skillArray = new int[] { skillATT, skillSTR, skillDEF, skillHIT,
				skillRNG, skillMAG, skillPRY, skillSLY };
		nameArray = new String[] { "Attack", "Strength", "Defense",
				"Hitpoints", "Range", "Magic", "Prayer", "Slayer" };

		return false;
	}

	boolean setupSkills = false;

	public void onRepaint(Graphics g) {
		long runTime = 0;
		long seconds = 0;
		long minutes = 0;
		long hours = 0;

		runTime = System.currentTimeMillis() - startTime;
		seconds = runTime / 1000;
		if (seconds >= 60) {
			minutes = seconds / 60;
			seconds -= (minutes * 60);
		}
		if (minutes >= 60) {
			hours = minutes / 60;
			minutes -= (hours * 60);
		}

		if (!setupSkills) {
			setupSkills();
			setupSkills = true;
		}

		int coordCount = 0;

		if (getCurrentTab() == TAB_INVENTORY) {
			g.setColor(new Color(0, 0, 0, 100));
			g.fillRoundRect(555, 210, 175, 250, 10, 10);
			g.setColor(Color.WHITE);

			int[] coords = new int[] { 225, 240, 255, 270, 285, 300, 315, 330,
					345, 360, 375, 390, 405, 420, 435, 450 };

			g.drawString("Run Time: " + hours + ":" + minutes + ":" + seconds,
					561, coords[1]);
			g.setColor(Color.WHITE);
			g.drawString("Games Completed: " + gamesCompleted, 561, coords[2]);
			g.setColor(Color.WHITE);
			g.drawString("Zeal Gained: " + zealGained, 561, coords[3]);

			skillATT = skills.getCurrentSkillExp(Skills.getStatIndex("attack"));
			skillSTR = skills.getCurrentSkillExp(Skills
					.getStatIndex("strength"));
			skillDEF = skills
					.getCurrentSkillExp(Skills.getStatIndex("defence"));
			skillRNG = skills.getCurrentSkillExp(Skills.getStatIndex("range"));
			skillMAG = skills.getCurrentSkillExp(Skills.getStatIndex("magic"));
			skillHIT = skills.getCurrentSkillExp(Skills
					.getStatIndex("hitpoints"));
			skillSLY = skills.getCurrentSkillExp(Skills.getStatIndex("slayer"));
			skillPRY = skills.getCurrentSkillExp(Skills.getStatIndex("prayer"));
			int[] newSkillArray = new int[] { skillATT, skillSTR, skillDEF,
					skillHIT, skillRNG, skillMAG, skillPRY, skillSLY };

			for (int i = 0; i < 8; i++) {
				xpgained = newSkillArray[i] - skillArray[i];
				if (xpgained == 0)
					continue;
				g.drawString(nameArray[i] + " XP: " + xpgained, 561,
						coords[(5 + coordCount)]);
				coordCount++;
			}

			g.drawString("Team Method: " + whatJoin, 561, coords[14]);
			g.drawString("In-Game Action: " + styleOfGame, 561, coords[15]);
		}

	}

	// Checks if we are in an area
	public boolean atArea(RSArea area) {
		if (area.contains(getMyPlayer().getLocation())) {
			return true;
		}
		return false;
	}

	// Clicks a NPC by Ruski
	public boolean clickNPC(RSNPC npc, String action) {
		if (npc == null) {
			return false;
		}
		RSTile tile = npc.getLocation();
		if (!tile.isValid()) {
			return false;
		}

		try {
			Point screenLoc = npc.getScreenLocation();
			if (distanceTo(tile) > 6 || !pointOnScreen(screenLoc)) {
				turnToTile(tile);
			}
			if (distanceTo(tile) > 20) {
				walkTileMM(tile);
				return false;
			}
			for (int i = 0; i < 12; i++) {
				screenLoc = npc.getScreenLocation();
				if (!npc.isValid() || !pointOnScreen(screenLoc)) {
					return false;
				}
				moveMouse(screenLoc, 5, 5);
				if (getMenuItems().get(0).toLowerCase().contains(
						npc.getName().toLowerCase())) {
					break;
				}
				if (getMouseLocation().equals(screenLoc)) {
					break;
				}
			}
			List<String> menuItems = getMenuItems();
			if (menuItems.isEmpty()) {
				return false;
			}
			for (int a = 0; a < menuItems.size(); a++) {
				if (menuItems.get(a).toLowerCase().contains(
						npc.getName().toLowerCase())) {
					if (menuItems.get(0).toLowerCase().contains(
							action.toLowerCase())) {
						clickMouse(true);
						return true;
					} else {
						clickMouse(false);
						return atMenu(action);
					}
				}
			}
		} catch (Exception e) {
			System.out.print("clickNPC(RSNPC, String) error: " + e);
			return false;
		}
		return false;
	}

	// Resets the values after each game
	public void reset() {
		deathTime = 0;
		currentTeam = null;
	}

	// Gets if the player is in an Area
	public boolean playerInArea(int maxX, int minY, int minX, int maxY) {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		if (x >= minX && x <= maxX && y >= minY && y <= maxY) {
			return true;
		}
		return false;
	}

	// Gets if the playa is in a Polygon Area
	public boolean playerInArea(Polygon area) {
		return area.contains(new Point(getMyPlayer().getLocation().getX(),
				getMyPlayer().getLocation().getY()));
	}

	// clickTile method by DDM
	public boolean clickTile(RSTile tile, String option) {
		Point p = Calculations.tileToScreen(tile);
		p.x += random(-5, 6);
		p.y += random(-5, 6);
		if (p.x < 0 || p.y < 0) {
			return false;
		}
		moveMouse(p);
		// wait(random(100,150));
		getMenuItems();
		@SuppressWarnings("unused")
		List<String> itemsList = getMenuItems();

		long waitTime = System.currentTimeMillis() + random(50, 250);
		boolean foundOption = false;
		while (System.currentTimeMillis() < waitTime && !foundOption) {
			wait(15);
			foundOption = (itemsList = getMenuItems()).get(0).toLowerCase()
					.contains(option.toLowerCase());
		}
		if (foundOption) {
			clickMouse(true);
			wait(random(150, 300));
			return true;
		}
		clickMouse(false);
		wait(random(150, 300));
		return atMenu(option);
	}

	// Method for clicking an inventoryItem
	public boolean clickInventoryItem(int itemID, String option) {
		if (getCurrentTab() != TAB_INVENTORY) {
			openTab(TAB_INVENTORY);
		}
		int[] items = getInventoryArray();
		java.util.List<Integer> possible = new ArrayList<Integer>();
		for (int i = 0; i < items.length; i++) {
			if (items[i] == itemID) {
				possible.add(i);
			}
		}
		if (possible.size() == 0) {
			return false;
		}
		int idx = possible.get(random(0, possible.size()));
		Point t = getInventoryItemPoint(idx);
		moveMouse(t, 5, 5);
		long waitTime = System.currentTimeMillis() + random(50, 250);
		boolean found = false;
		while (!found && System.currentTimeMillis() < waitTime) {
			wait(15);
			if ((getMenuItems()).get(0).toLowerCase().contains(
					option.toLowerCase())) {
				found = true;
			}
		}
		if (found) {
			clickMouse(true);
			wait(random(150, 250));
			return true;
		}
		clickMouse(false);
		wait(random(150, 250));
		return atMenu(option);
	}

	public void setRetaliateOff() {
		if (isRetaliateEnabled()) {
			if (getCurrentTab() != TAB_ATTACK) {
				openTab(TAB_ATTACK);
			}
			wait(random(1000, 2000));
			try {
				moveMouse(random(590, 690), random(370, 390));
				wait(random(500, 1000));
				clickMouse(true);
				log("Set auto-retaliate OFF");
			} catch (Exception e) {

			}
		}
		return;
	}

	// Kills players at between a maxDistance
	public RSPlayer killPlayer(final int maxDistance) {
		RSPlayer playa = null;
		ArrayList<RSPlayer> playersList = new ArrayList<RSPlayer>();
		int[] validPlayers = Bot.getClient().getPlayerIndexArray();
		final Player[] players = Bot.getClient().getPlayerArray();

		for (final int element : validPlayers) {
			if (element >= players.length)
				continue;
			if (players[element] == null) {
				continue;
			}
			playa = new RSPlayer(players[element]);
			RSTile playaTile = playa.getLocation();
			try {
				if (distanceTo(playa) < maxDistance && playersList.size() >= 10
						&& playersList.size() <= 25) {
					playersList.add(playa);
				}

				RSTile targetLoc = playa.getLocation();
				Point checkPlayer = Calculations.tileToScreen(targetLoc);
				if (!pointOnScreen(checkPlayer)) {
					continue;
				}
				if (getMyPlayer().getTeam() != playa.getTeam()
						&& !getMyPlayer().isInCombat()
						&& getMyPlayer().getInteracting() == null) {
					validPlayers = Bot.getClient().getPlayerIndexArray();
					doSpec();
					clickTile(playaTile, "Attack");
				} else if (!pointOnScreen(checkPlayer)) {
					walkTileMM(targetLoc);
				}
			} catch (final Exception e) {
				// e.printStackTrace(); TODO: Why?
			}
		}
		return playersList.get(random(0, playersList.size()));
	}

	public boolean isOutGame() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x >= 1872 && x <= 1899 && y >= 3153 && y <= 3182;
	}

	// Bandages players
	public RSPlayer bandagePlaya(final int maxDistance) {
		RSPlayer player = null;
		ArrayList<RSPlayer> playersList = new ArrayList<RSPlayer>();
		int[] validPlayers = Bot.getClient().getPlayerIndexArray();
		final Player[] players = Bot.getClient().getPlayerArray();

		for (final int element : validPlayers) {
			if (players[element] == null) {
				continue;
			}
			player = new RSPlayer(players[element]);
			RSTile playaTile = player.getLocation();
			try {
				Point checkPlayer = Calculations.tileToScreen(player
						.getLocation());
				if (distanceTo(player) > 10) {
					continue;
				}
				if (distanceTo(player) < maxDistance
						&& playersList.size() >= 10 && playersList.size() <= 25) {
					playersList.add(player);
				}
				if (!pointOnScreen(checkPlayer)) {
					continue;
				}
				if (isOutGame() || !inGame) {
					clickContinue();
					inGame = false;
					break;
				}
				if (!inventoryContains(14640) || isBlueStartingArea()
						|| isRedStartingArea()) {
					break;
				}
				if (inventoryContains(14640)) {
					clickInventoryItem(14640, "use");
					wait(random(700, 900));
					validPlayers = Bot.getClient().getPlayerIndexArray();
					String name = player.getName();
					moveMouse(checkPlayer, 2, 2);
					wait(random(500, 750));
					validPlayers = Bot.getClient().getPlayerIndexArray();
					clickTile(playaTile, "use bandages -> " + name);
				} else {
					break;
				}
			} catch (Exception e) {
				log(e.toString());
			}
		}
		return playersList.get(random(0, playersList.size()));
	}

	public boolean useBandagesOnOtherPlayer() {
		int distanc = 2;
		RSPlayer closest = null;
		int[] validPlayers = Bot.getClient().getPlayerIndexArray();
		Player[] players = Bot.getClient().getPlayerArray();

		for (int element : validPlayers) {
			if (players[element] == null) {
				continue;
			}
			RSPlayer player = new RSPlayer(players[element]);
			try {
				int distance = distanceTo(player);
				if (distance < distanc) {
					distanc = distance;
					closest = player;
				}
				RSTile targetLoc = closest.getLocation();
				Point checkPlayer = Calculations.tileToScreen(targetLoc);
				if (pointOnScreen(checkPlayer) && !getMyPlayer().isInCombat()) {
					if (getInventoryCount(14640) > 0) {
						clickInventoryItem(14640, "use");
						wait(random(1000, 3500));
						String name = closest.getName();
						moveMouse(checkPlayer, 2, 2);
						wait(random(500, 750));
						clickMouse(false);
						atMenu("Use Bandages -> " + name);
						wait(random(10000, 25000));
					} else {
						break;
					}
				} else {
					break;
				}
			} catch (Exception e) {
				log(e.toString());
			}
		}
		return closest != null;
	}

	public boolean atSoulWars() {
		if (playerInArea(1800, 3162, 1899, 3165) && !atRedWait()
				&& !atBlueWait()) {
			Wayoo = "SoulWars";
			return true;
		}
		return false;
	}

	public boolean atRedWait() {
		if (playerInArea(1900, 3159, 1910, 3165)) {
			Wayoo = "In Red Waiting Area";
			return true;
		}
		return false;
	}

	public boolean atBlueWait() {
		if (playerInArea(1870, 3158, 1879, 3165)) {
			Wayoo = "In Blue Waiting Area";
			return true;
		}
		return false;
	}

	public boolean atRedGraveyard() {
		if (playerInArea(1951, 3234, 1958, 3244)) {
			Wayoo = "In Red Graveyard";
			return true;
		}
		return false;
	}

	public boolean atBlueGraveyard() {
		if (playerInArea(1816, 3122, 1822, 3230)) {
			Wayoo = "In Blue Graveyard";
			return true;
		}
		return false;
	}

	public boolean atRedBandaids() {
		if (playerInArea(1964, 3207, 1973, 3211)) {
			Wayoo = "In Red Bandaids";
			return true;
		}
		return false;
	}

	public boolean atBlueBandaids() {
		if (playerInArea(1798, 3251, 1809, 3255)) {
			Wayoo = "In Blue Bandaids";
			return true;
		}
		return false;
	}

	public int AFKBlue() {
		if (AFK && !isRedStartingArea() && !isBlueStartingArea()) {
			if (getInventoryCount(bandageID) > 0 && !getMyPlayer().isInCombat()
					&& !getMyPlayer().isMoving()) {
				bandagePlaya(2);
			} else if (!inventoryContains(14640) && inGame) {
				theStuff = "WalkTo Bandages";
			} else if (bandageTableBlue != null) {
				theStuff = "Pick Bandages";
			}
		}

		if (theStuff.equals("WalkTo Bandages") && !isRedStartingArea()
				&& !isBlueStartingArea()) {
			if (!getMyPlayer().isMoving()
					|| distanceTo(getDestination()) <= random(5, 8)) {
				walkPathMM(randomizePath(BlueToBandage, 2, 2), 16);
			}
		}

		if (theStuff.equals("Pick Bandages") && !isRedStartingArea()
				&& !isBlueStartingArea()) {
			if (distanceTo(bandageTableBlue) < 3 && !getMyPlayer().isMoving()) {
				clickTile(bandageTableBlue, "Take-x");
				wait(random(1500, 3000));
				if (RSInterface.getInterface(752).getChild(4).isValid()
						&& RSInterface.getInterface(752).getChild(4)
								.containsText("How many bandages would")) {
					enterNumber();
					wait(random(600, 950));
				} else {
					walkTileMM(randomizeTile(new RSTile(1810, 3254), 2, 2));
					wait(random(3000, 5000));
				}
			}
		}

		return random(50, 150);
	}

	public int AFKRed() {
		if (AFK && !isRedStartingArea() && !isBlueStartingArea()) {
			if (getInventoryCount(bandageID) > 0 && !getMyPlayer().isInCombat()
					&& !getMyPlayer().isMoving()) {
				bandagePlaya(2);
			} else if (!inventoryContains(14640) && inGame) {
				theStuff = "WalkTo Bandages";
			} else if (bandageTableBlue != null) {
				theStuff = "Pick Bandages";
			}
		}

		if (theStuff.equals("WalkTo Bandages") && !isRedStartingArea()
				&& !isBlueStartingArea()) {
			if (!getMyPlayer().isMoving()
					|| distanceTo(getDestination()) <= random(5, 8)) {
				walkPathMM(randomizePath(RedToBandage, 2, 2), 16);
			}
		}

		if (theStuff.equals("Pick Bandages") && !isRedStartingArea()
				&& !isBlueStartingArea()) {
			if (distanceTo(bandageTableRed) < 3 && !getMyPlayer().isMoving()) {
				clickTile(bandageTableRed, "Take-x");
				wait(random(1500, 3000));
				if (RSInterface.getInterface(752).getChild(4).isValid()
						&& RSInterface.getInterface(752).getChild(4)
								.containsText("How many bandages would")) {
					enterNumber();
					wait(random(600, 950));
				} else {
					walkTileMM(randomizeTile(new RSTile(1963, 3207), 2, 2));
					wait(random(3000, 5000));
				}
			}
		}

		return random(50, 150);
	}

	public int FightPyres(RSTile[] path) {
		if (FIGHT && !isRedStartingArea() && !isBlueStartingArea()) {
			if (!getMyPlayer().isInCombat() && !getMyPlayer().isMoving()
					&& distanceTo(PyreFiend) <= 5 && !isAtObelisk()
					&& !isRedGraveyard() && !isBlueStartingArea()
					&& !isRedStartingArea() && !isBlueGraveyard()) {
				clickNPC(PyreFiend, "Attack");
			} else if (!isRedGraveyard() && !isBlueStartingArea()
					&& !isRedStartingArea() && !isAtObelisk()
					&& !isBlueGraveyard() && !infrontOfBlueGraveyard()
					&& !infrontOfRedGraveyard()) {
				theStuff = "WalkTo Pyres";
			}
		}

		if (theStuff.equals("WalkTo Pyres") && !isRedGraveyard()
				&& !isBlueStartingArea() && !isRedStartingArea()
				&& !isAtObelisk() && !isBlueGraveyard()
				&& !getMyPlayer().isInCombat()) {
			if (!getMyPlayer().isMoving()
					|| distanceTo(getDestination()) <= random(5, 8)) {
				setCameraAltitude(true);
				walkPathMM(randomizePath(path, 2, 2), 16);
			}
		}

		return random(50, 150);
	}

	public int KillPlayers(RSTile[] path) {
		if (KILL && !isRedStartingArea() && !isBlueStartingArea()) {
			if (!getMyPlayer().isInCombat() && !getMyPlayer().isMoving()
					&& isAtObelisk() && !isRedGraveyard()
					&& !isBlueStartingArea() && !isRedStartingArea()
					&& !isBlueGraveyard()) {
				killPlayer(5);
			} else if (!isRedGraveyard() && !isBlueStartingArea()
					&& !isRedStartingArea() && !isAtObelisk()
					&& !isBlueGraveyard() && !infrontOfBlueGraveyard()
					&& !infrontOfRedGraveyard()) {
				theStuff = "WalkTo Obelisk";
			}
		}

		if (theStuff.equals("WalkTo Obelisk") && !isRedGraveyard()
				&& !isBlueStartingArea() && !isRedStartingArea()
				&& !isAtObelisk() && !isBlueGraveyard()
				&& !getMyPlayer().isInCombat()) {
			if (!getMyPlayer().isMoving()
					|| distanceTo(getDestination()) <= random(5, 8)) {
				setCameraAltitude(true);
				walkPathMM(randomizePath(path, 2, 2), 16);
			}
		}

		return random(50, 150);
	}

	public int Suicide(RSTile[] path) {
		if (SUICIDE && !isRedStartingArea() && !isBlueStartingArea()) {
			if (!isRedGraveyard() && !isBlueStartingArea()
					&& !isRedStartingArea() && !isAtObelisk()
					&& !isBlueGraveyard() && !infrontOfBlueGraveyard()
					&& !infrontOfRedGraveyard()) {
				theStuff = "WalkTo Avatar";
			}
		}

		if (theStuff.equals("WalkTo Avatar") && !isRedGraveyard()
				&& !isBlueStartingArea() && !isRedStartingArea()
				&& !isAtObelisk() && !isBlueGraveyard()
				&& !getMyPlayer().isInCombat()) {
			if (!getMyPlayer().isMoving()
					|| distanceTo(getDestination()) <= random(5, 8)) {
				setCameraAltitude(true);
				walkPathMM(randomizePath(path, 2, 2), 16);
			}
		}
		return random(50, 150);
	}

	public boolean isAtBlueRoom() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x >= 1870 && x <= 1879 && y >= 3159 && y <= 3165;
	}

	public boolean isAtRedRoom() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x >= 1900 && x <= 1909 && y >= 3157 && y <= 3165;
	}

	public boolean isBlueStartingArea() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x >= 1816 && x <= 1823 && y >= 3220 && y <= 3230;
	}

	public boolean isRedStartingArea() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x >= 1952 && x <= 1958 && y >= 3234 && y <= 3243;
	}

	public boolean isBlueGraveyard() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x >= 1841 && x <= 1843 && y >= 3217 && y <= 3219;
	}

	public boolean isRedGraveyard() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x >= 1932 && x <= 1934 && y >= 3244 && y <= 3246;
	}

	public boolean isAtObelisk() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x >= 1873 && x <= 1900 && y >= 3220 && y <= 3245;
	}

	public boolean infrontOfRedGraveyard() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x == 1933 && y == 3243;
	}

	public boolean infrontOfBlueGraveyard() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x == 1842 && y == 3220;
	}

	public boolean outsideBlueGameRoom() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x == 1815 && y == 3225;
	}

	public boolean outsideRedGameRoom() {
		int x = getMyPlayer().getLocation().getX();
		int y = getMyPlayer().getLocation().getY();
		return x == 1959 && y == 3239;
	}

}